import requests
import json
from common.Log import logger

logger = logger


def send_get(url, data):
    result = requests.get(url=url, data=data)
    res = json.dumps(result, ensure_ascii=False, sort_keys=True, indent=2)
    return res


def send_post(url, data):  # 定义一个方法，传入需要的参数url和data
    # 参数必须按照url、data顺序传入
    result = requests.post(url=url, data=data).json()  # 因为这里要封装post方法，所以这里的url和data值不能写死
    res = json.dumps(result, ensure_ascii=False, sort_keys=True, indent=2)
    return res


def run_main(method, url=None, data=None):  # 定义一个run_main函数，通过传过来的method来进行不同的get或post请求
    result = None
    if method == 'post':
        result = send_post(url, data)
        logger.info(str(result))
    elif method == 'get':
        result = send_get(url, data)
        logger.info(str(result))
    else:
        print("method值错误！！！")
        logger.info("method值错误！！！")
    return result


def send_postjson():
    a = {"userName": "ceshi", "password": "hn.2020@QGC"}
    url = "http://192.168.3.147:38080/basics/account/userLogin"
    # 消息头指定
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    # 发送post请求 json参数直接为一个字典数据。
    res = requests.request("post", url, json=a, headers=headers)
    print(res.status_code)
    print(res.text)


class RunMain:
    pass


if __name__ == '__main__':  # 通过写死参数，来验证我们写的请求是否正确
    result = run_main('post', 'http://192.168.3.147:38080/basics/account/userLogin', '{"userName": "ceshi", "password": "hn.2020@QGC"}')
    print(result)
    # send_postjson()
